﻿using System;
using System.ComponentModel;
using System.Security;
using System.Windows.Forms;
using Plugins.Library;

namespace Plugins.Controls
{
    public static class PluginInvoke
    {
        //[SecurityPermission(SecurityAction.Demand, Execution = true)]
        //[UIPermission(SecurityAction.Demand, Window = UIPermissionWindow.AllWindows)]
        public static void ContainerAction<T>(T control, Action<T> method) where T : ContainerControl
        {
            try
            {
                if (control != null && method != null)
                {
                    if (control.InvokeRequired)
                    {
                        if (control.IsHandleCreated && !(control.Disposing || control.IsDisposed))
                        {
                            control.Invoke(method, control);
                        }
                    }
                    else
                    {
                        if (!(control.Disposing || control.IsDisposed))
                        {
                            method(control);
                        }
                    }
                }
            }
            catch (ArgumentNullException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (SecurityException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (AccessViolationException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (ApplicationException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (FormatException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (ArgumentException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (UnauthorizedAccessException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (InvalidOperationException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (Win32Exception ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
        }
        //[SecurityPermission(SecurityAction.Demand, Execution = true)]
        //[UIPermission(SecurityAction.Demand, Window = UIPermissionWindow.AllWindows)]
        public static void Action<T>(T control, Action<T> method) where T : Control
        {
            try
            {
                if (control != null && method != null)
                {
                    if (control.InvokeRequired)
                    {
                        if (control.IsHandleCreated && !(control.Disposing || control.IsDisposed))
                        {
                            control.Invoke(method, control);
                        }
                    }
                    else
                    {
                        if (!(control.Disposing || control.IsDisposed))
                        {
                            method(control);
                        }
                    }
                }
            }
            catch (ArgumentNullException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (SecurityException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (AccessViolationException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (ApplicationException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (FormatException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (ArgumentException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (UnauthorizedAccessException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (InvalidOperationException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            catch (Win32Exception ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
        }
    }
}
